package cn.icanci.loopstack.bic.dal.mongo.mongo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.NodeDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.NodeDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/10 15:29
 */
@Service("nodeDAO")
public class MongoNodeDAO extends AbstractBaseDAO<NodeDO> implements NodeDAO {
    @Override
    public void insert(NodeDO node) {
        super.insert(node);
        mongoTemplate.insert(node, COLLECTION_NAME);
    }

    @Override
    public void update(NodeDO node) {
        super.update(node);
        mongoTemplate.save(node, COLLECTION_NAME);
    }

    @Override
    public List<NodeDO> queryAll() {
        Criteria criteria = Criteria.where(NodeColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public PageList<NodeDO> pageQuery(NodeDO node, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(NodeColumn.ENV).is(DEFAULT_ENV);
        if (StringUtils.isNotBlank(node.getNodeName())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(NodeColumn.NODE_NAME).regex("^.*" + node.getNodeName() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(node.getNodeKey())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(NodeColumn.NODE_KEY).regex("^.*" + node.getNodeKey() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(node.getSystemUuid())) {
            criteria.and(NodeColumn.SYSTEM_UUID).is(node.getSystemUuid());
        }
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, NodeColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    @Override
    public NodeDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(NodeColumn._ID).is(_id);
        criteria.and(NodeColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return NodeColumn.class;
    }

    @Override
    public NodeDO queryBySystemUuidAndName(String systemUuid, String name) {
        Criteria criteria = Criteria.where(NodeColumn.NODE_NAME).is(name);
        criteria.and(NodeColumn.ENV).is(DEFAULT_ENV);
        criteria.and(NodeColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public NodeDO queryBySystemUuidAndKey(String systemUuid, String key) {
        Criteria criteria = Criteria.where(NodeColumn.NODE_KEY).is(key);
        criteria.and(NodeColumn.ENV).is(DEFAULT_ENV);
        criteria.and(NodeColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public NodeDO queryByUuid(String nodeUuid) {
        Criteria criteria = Criteria.where(NodeColumn.UUID).is(nodeUuid);
        criteria.and(NodeColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }
}
